package linear_list.nowcoder.medium;

import java.util.Scanner;

/**
 * @author bruin_du
 * @description 反转部分单向链表
 * https://www.nowcoder.com/questionTerminal/f11155006f154419b0bef6de8918aea2
 * @date 2022/5/25 21:11
 **/
public class ReversePartOfList {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()) {
            int n = in.nextInt();
            ListNode head = null, tail = null;
            for (int i = 0; i < n; i++) {
                ListNode node = new ListNode(in.nextInt());
                if (head == null) {
                    head = node;
                    tail = node;
                } else {
                    tail.next = node;
                    tail = node;
                }
            }
            int left = in.nextInt();
            int right = in.nextInt();
            ListNode prev = null, cur = head, next = head;
            ListNode newHead = null, newTail = null;
            if (n != 1 && (right - left != 0)) {
                for (int i = 1; i < left; i++) {
                    prev = cur;
                    cur = next = cur.next;
                }
                for (int i = left; i <= right; i++) {
                    next = cur.next;
                    cur.next = newHead;
                    newHead = cur;
                    if (newHead.next == null)
                        newTail = newHead;
                    cur = next;
                }
                if (prev != null) {
                    prev.next = newHead;
                    newHead = head;
                }
                if (newTail != null)
                    newTail.next = next;
            }
            ListNode tmp = newHead == null ? head : newHead;
            while (tmp != null) {
                System.out.print(tmp.val);
                if (tmp.next != null)
                    System.out.print(' ');
                tmp = tmp.next;
            }
            System.out.println();
        }
    }
}
